<?php

Class IngredientsController extends AppController {

    public $name = 'Ingredients';

    function beforeFilter() {

        $this->Auth->allow('search');
    }

    function add() {
        $this->layout = "registered";
        if (empty($this->data)) {
            
        } else {
            if ($this->Ingredient->save($this->data)) {
                $this->Session->setFlash('Registro Adicionado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Ingredients', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Datenfehler', 'flash_custom');
            }
        }
    }

    function edit($id) {
        $this->layout = "registered";
        $this->Ingredient->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Ingredient->find('first', array('conditions' => array('Ingredient.id' => $id), 'fields' => array('Ingredient.id', 'Ingredient.type', 'Ingredient.name', 'Ingredient.description', 'Ingredient.id')));
        } else {
            if ($this->Ingredient->save($this->data)) {
                $this->Session->setFlash('Registro editado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Ingredients', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Datenfehler', 'flash_custom');
            }
        }
    }

    function index() {
        $this->layout = "registered";
        $this->paginate = array('Ingredient' => array('maxLimit' => 500, 'limit' => 10, 'fields' => array('Ingredient.type', 'Ingredient.name', 'Ingredient.description', 'Ingredient.id')));
        $this->set('Ingredients', $this->paginate());
    }

    function search() {
        $this->layout = "index_default";
        $this->set('ingredientes', $this->Ingredient->find('all', array('recursive' => -1)));
        if (!empty($this->data)) {
            
        }
    }

    function view() {
        App::Import('model', 'Cocktail');
        $Cocktail = new Cocktail();
        $cocktails = $Cocktail->find('all', array('recursive' => -1, 'fields' => array('Cocktail.id', 'Cocktail.description', 'Cocktail.name', 'Cocktail.image')));
        $cocktailList = array();
        foreach ($cocktails as $cocktail) {

            $countIngredients = $Cocktail->CocktailIngredient->find('count', array('conditions' => array('CocktailIngredient.cocktail_id' => $cocktail['Cocktail']['id'])));

            //Puede estar completo o contenerlo
            $Ingredients = $Cocktail->CocktailIngredient->find('all', array('conditions' => array('CocktailIngredient.cocktail_id' => $cocktail['Cocktail']['id'])));
            $total = 0;
            foreach ($this->data['Ingredientes'] as $value) {

                foreach ($Ingredients as $ingredient) {
                    if ($value == $ingredient['CocktailIngredient']['ingredient_id']) {
                        $total++;
                    }
                }
            }
            if (($total + $this->data['Ingredient']['filtro'] >= $countIngredients ) AND $total != 0) {
                $cocktailList[] = $cocktail;
            }
        }
        if (empty($cocktailList)) {
            $this->Session->SetFlash('Leider kein Ergebnis', 'flash_custom');
        }
        $this->set('cocktailList', $cocktailList);
    }

    function delete($id) {
        if ($this->Ingredient->delete($id, true)) {
            $this->Session->setFlash(_("Ingrediente eliminado con éxito"), "flash_custom");
            $this->redirect(array('controller' => 'Ingredients', 'action' => 'index'));
        }
    }

}

?>